//
//  VCMusicListViewController.m
//  VideoCollage
//
//  Created by Mac on 2018/6/25.
//  Copyright © 2018年 stoneobs.qq.com. All rights reserved.
//

#import "VCMusicListViewController.h"
#import "VCMusicListTableViewCell.h"
#import "VCMusicModel.h"
@interface VCMusicListViewController ()
@property(nonatomic, strong) NSMutableArray<VCMusicModel*>                     *dataSouce;/**< <##> */
@end

@implementation VCMusicListViewController

- (void)dealloc{
    DDLogInfo(@"VCMusicListViewController dealloc");
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"Music";
    NSArray * musciList = [VCBgmManger manger].allBgm;
    self.dataSouce = NSMutableArray.new;
    for (NSString * name in musciList) {
        VCMusicModel * model = VCMusicModel.new;
        model.name = name;
        model.dealName = [name componentsSeparatedByString:@"."].firstObject;;
        model.isSlected = NO;
        model.path = [VCBgmManger realPath:name];
        [self.dataSouce addObject:model];
    }
    [self.tableView reloadData];
    // Do any additional setup after loading the view.
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [[VCBgmManger manger] pause];
}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.dataSouce.count ;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return VCMusicListTableViewCell.cellHeight;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 10;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    VCMusicListTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[VCMusicListTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        
    }
    VCMusicModel * model =  self.dataSouce[indexPath.section];
    cell.model = model;
    __weak typeof(self) weakSelf =  self;
    [cell.itemButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelctedItemButtonWithIndexPath:indexPath];
    }];
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    VCMusicModel * model =  self.dataSouce[indexPath.section];
    NSString * name = model.name;
    NSString * path = model.path;
    if (model.isSlected) {
         [[VCBgmManger manger] pause];
        model.isSlected = NO;
        
    }else{
        for (VCMusicModel * existModel in self.dataSouce) {
            existModel.isSlected = NO;
        }
        model.isSlected = YES;
         [[VCBgmManger manger] playWithPath:path];
    }
    [self.tableView reloadData];

    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}
#pragma mark --Action Method
- (void)onSelctedItemButtonWithIndexPath:(NSIndexPath*)indexPath{
    VCMusicModel * model =  self.dataSouce[indexPath.section];
    if (model.isSlected) {
        
        if (self.onSelctedMusciHandle) {
            [[VCBgmManger manger] pause];
            self.onSelctedMusciHandle(model.dealName, model.path);
            [self.navigationController popViewControllerAnimated:YES];
        }
        
    }else{
        for (VCMusicModel * existModel in self.dataSouce) {
            existModel.isSlected = NO;
        }
        model.isSlected = YES;
        [[VCBgmManger manger] playWithPath:model.path];
    }
      [self.tableView reloadData];
}

@end

